#include "Comm.hpp"
#include "Fifo.hpp"

int main()
{
    // 1.获取key
    key_t key = GetShmKey();
    cout << "key: "<< ToHex(GetShmKey()) << endl;
    //sleep(2);

    // 2.获取共享内存
    int shmid = GetShm(key, defaultsize);
    cout << "shmid: "<< shmid << endl;
    //sleep(2);

    // 3.将共享内存和进程进程挂接
    char* addr = (char*)ShmAttach(shmid);
    cout << "Attch shm success, addr: " << ToHex((uint64_t)addr) << endl;
    //sleep(5);

    Sync syn;
    syn.OpenWrite();

    // ShmClient - 客户端
    // 此时就可以进行通信了
    memset(addr, 0, defaultsize);//清空共享内存
    // pipe,fifo,->read,write->系统调用,shm->没有使用任何系统调用
    for(char c = 'A'; c < 'Z'; c++)
    {
        addr[c-'A'] = c;//向共享内存写入消息
        sleep(1);
        syn.WakeUp();
    }

    // 4.删除映射关系
    ShmDetach(addr);
    cout << "Detach shm success, addr: " << ToHex((uint64_t)addr) << endl;
    sleep(5);


    return 0;
}